<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';


class dao_seat {

    //Methods
    public function __construct() {

    }

    public function get_all() {
        $db = new connection();
        $con = $db->open_connect();
        
        $query = "SELECT * "
                . "FROM tbl_seat ";
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());
        
        $list = array();
        while ($row_pos = mysqli_fetch_array($result)) {
            array_push($list, $row_pos);
        }
        $db->close_connect();
        return $list;
    }

    public function get_all_columns($ticket_id) {
        $db = new connection();
        $con = $db->open_connect();
        
        $query = "SELECT distinct s.column_pos "
                . "FROM tbl_seat s "
                . "WHERE s.ticket_id = " . $ticket_id . " "
                . "ORDER BY s.column_pos ASC";
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());
        
        $list = array();
        while ($row_pos = mysqli_fetch_array($result)) {
            array_push($list, $row_pos);
        }
        $db->close_connect();
        return $list;
    }
    
    public function get_all_rows($ticket_id) {
        $db = new connection();
        $con = $db->open_connect();
        
        $query = "SELECT distinct row_pos "
                . "FROM tbl_seat "
                . "WHERE ticket_id = " . $ticket_id . " "
                . "ORDER BY row_pos ASC";
        $result = mysqli_query($con, $query)
                
        or die("Query fail: " . mysqli_error());
        
        $list = array();
        while ($row_pos = mysqli_fetch_array($result)) {
            array_push($list, $row_pos);
        }
        $db->close_connect();
        return $list;
    }
    
    public function save($id, $row_pos, $column_pos, $ticket_id, $seat_status_id, $is_wheelchair) {
        $db = new connection();
        $con = $db->open_connect();
        
        if ($id == 0) { 
            $query = "INSERT INTO tbl_seat(row_pos, column_pos, ticket_id, seat_status_id, is_wheelchair) VALUES (
                '" . $row_pos . "','" . $column_pos . "'," . $ticket_id . "," . $seat_status_id . "," . $is_wheelchair . ")";
        } else { // no update is_wheelchair
            $query = "UPDATE tbl_seat SET "
                    . "row_pos = '" . $row_pos . "',"
                    . "column_pos = '" . $column_pos . "',"
                    . "ticket_id = " . $ticket_id . ","
                    . "seat_status_id = " . $seat_status_id . " "
                    . "WHERE id = " . $id;
        }
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }
    
    public function update_seat_status($id, $seat_status_id) {
        $db = new connection();
        $con = $db->open_connect();
        
        if ($id != 0) { 
            $query = "UPDATE tbl_seat SET "
                    . "seat_status_id = " . $seat_status_id . " "
                    . "WHERE id = " . $id;
        }
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }
    
    public function get_by_id($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * "
                . "FROM tbl_seat "
                . "WHERE id = " . $id;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row_pos = mysqli_fetch_array($result);

        $db->close_connect();
        return $row_pos;
    }

    public function get_by_position($ticket_id, $row_pos, $column_pos) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT s.*, ss.image AS seat_status_image, ss.id AS seat_status_id "
                . "FROM tbl_seat s LEFT JOIN tbl_seat_status ss ON s.seat_status_id = ss.id "
                . "WHERE s.ticket_id = " . $ticket_id . " "
                . "AND s.row_pos = '" . $row_pos . "' "
                . "AND s.column_pos = '" . $column_pos . "' ";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row_pos = mysqli_fetch_array($result);

        $db->close_connect();
        return $row_pos;
    }
    
    public function delete($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_seat WHERE id = " . $id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }
    
}
